
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>ContentProvider · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="BroadcastReceiver.html" />
    
    
    <link rel="prev" href="Service.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../">
            
                <a href="../">
            
                    
                    Android
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Activity.md">
            
                <span>
            
                    
                    四大组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="Activity.html">
            
                <a href="Activity.html">
            
                    
                    Activity
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2" data-path="Service.html">
            
                <a href="Service.html">
            
                    
                    Service
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.1.3" data-path="ContentProvider.html">
            
                <a href="ContentProvider.html">
            
                    
                    ContentProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4" data-path="BroadcastReceiver.html">
            
                <a href="BroadcastReceiver.html">
            
                    
                    BroadcastReceiver
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Fragment.html">
            
                <a href="../Fragment.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../Context.html">
            
                <a href="../Context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../View.html">
            
                <a href="../View.html">
            
                    
                    View
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../动画.html">
            
                <a href="../动画.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../WebView.html">
            
                <a href="../WebView.html">
            
                    
                    WebView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../ListView与RecyclerView.html">
            
                <a href="../ListView与RecyclerView.html">
            
                    
                    ListView与RecyclerView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../线程.html">
            
                <a href="../线程.html">
            
                    
                    线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../进程.html">
            
                <a href="../进程.html">
            
                    
                    进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../序列化.html">
            
                <a href="../序列化.html">
            
                    
                    序列化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../图片加载与Bitmap.html">
            
                <a href="../图片加载与Bitmap.html">
            
                    
                    图片加载与Bitmap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../内存泄漏与内存溢出.html">
            
                <a href="../内存泄漏与内存溢出.html">
            
                    
                    内存泄漏与内存溢出
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../ANR.html">
            
                <a href="../ANR.html">
            
                    
                    ANR
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../SparseArray.html">
            
                <a href="../SparseArray.html">
            
                    
                    SparseArray
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../网络.html">
            
                <a href="../网络.html">
            
                    
                    网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../性能优化.html">
            
                <a href="../性能优化.html">
            
                    
                    性能优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.17" data-path="../适配.html">
            
                <a href="../适配.html">
            
                    
                    适配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.18" data-path="../架构设计.html">
            
                <a href="../架构设计.html">
            
                    
                    架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.19" data-path="../Java虚拟机与Dalvik、ART虚拟机的区别.html">
            
                <a href="../Java虚拟机与Dalvik、ART虚拟机的区别.html">
            
                    
                    Java虚拟机与Dalvik、ART虚拟机的区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.20" data-path="../ActivityThread、AMS、WMS的工作原理.html">
            
                <a href="../ActivityThread、AMS、WMS的工作原理.html">
            
                    
                    ActivityThread、AMS、WMS的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.21" data-path="../混合开发.html">
            
                <a href="../混合开发.html">
            
                    
                    混合开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.22" data-path="../插件化.html">
            
                <a href="../插件化.html">
            
                    
                    插件化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.23" data-path="../第三方库.html">
            
                <a href="../第三方库.html">
            
                    
                    第三方库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../Java/REAM.html">
            
                <a href="../../Java/REAM.html">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../../Java/Java基础.html">
            
                <a href="../../Java/Java基础.html">
            
                    
                    Java基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../../Java/JVM.html">
            
                <a href="../../Java/JVM.html">
            
                    
                    JVM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../Java/数据结构.html">
            
                <a href="../../Java/数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../../Java/多线程.html">
            
                <a href="../../Java/多线程.html">
            
                    
                    多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../../Java/应用类型的区别与使用场景.html">
            
                <a href="../../Java/应用类型的区别与使用场景.html">
            
                    
                    引用类型的区别与使用场景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../../Java/异常处理.html">
            
                <a href="../../Java/异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="../../Java/设计模式.html">
            
                <a href="../../Java/设计模式.html">
            
                    
                    设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="../../Java/算法.html">
            
                <a href="../../Java/算法.html">
            
                    
                    算法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../Http/">
            
                <a href="../../Http/">
            
                    
                    网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../Http/网络分层.html">
            
                <a href="../../Http/网络分层.html">
            
                    
                    网络分层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../Http/http.html">
            
                <a href="../../Http/http.html">
            
                    
                    Http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../Http/Https.html">
            
                <a href="../../Http/Https.html">
            
                    
                    Https
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../Http/TCP.html">
            
                <a href="../../Http/TCP.html">
            
                    
                    TCP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../Http/Socket.html">
            
                <a href="../../Http/Socket.html">
            
                    
                    Socket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >ContentProvider</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="contentprovider">ContentProvider</h1>
<h3 id="&#x8C08;&#x8C08;&#x4F60;&#x5BF9;-contentprovider-&#x7684;&#x7406;&#x89E3;">&#x8C08;&#x8C08;&#x4F60;&#x5BF9; ContentProvider &#x7684;&#x7406;&#x89E3;</h3>
<p>ContentProvider &#x662F; Android &#x4E2D;&#x63D0;&#x4F9B;&#x7684;&#x4E13;&#x95E8;&#x7528;&#x4E8E;&#x4E0D;&#x540C;&#x5E94;&#x7528;&#x95F4;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x5171;&#x4EAB;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x662F;&#x4E00;&#x79CD;&#x5185;&#x5BB9;&#x5171;&#x4EAB;&#x578B;&#x7EC4;&#x4EF6;&#xFF0C;&#x5E95;&#x5C42;&#x901A;&#x8FC7; Binder &#x5B9E;&#x73B0;&#xFF0C;&#x53EF;&#x4EE5;&#x5411;&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;&#x6216;&#x5176;&#x4ED6;&#x5E94;&#x7528;&#x63D0;&#x4F9B;&#x6570;&#x636E;&#xFF0C;&#x9002;&#x5408;&#x8FDB;&#x7A0B;&#x95F4;&#x901A;&#x4FE1;&#x3002;</p>
<p>ContentProvider &#x4E3B;&#x8981;&#x4EE5;&#x8868;&#x683C;&#x5F62;&#x5F0F;&#x6765;&#x7EC4;&#x7EC7;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x591A;&#x4E2A;&#x8868;&#x3002;ContentProvider &#x5BF9;&#x5E95;&#x5C42;&#x7684;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;&#x6CA1;&#x6709;&#x8981;&#x6C42;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; SQLite&#x3001;&#x6587;&#x4EF6;&#x3001;&#x5185;&#x5B58;&#x4E2D;&#x7684;&#x5BF9;&#x8C61;&#x7B49;&#x3002;</p>
<h3 id="contentprovider-&#x7684;&#x57FA;&#x672C;&#x4F7F;&#x7528;">ContentProvider &#x7684;&#x57FA;&#x672C;&#x4F7F;&#x7528;</h3>
<p>&#x521B;&#x5EFA; ContentProvider &#xFF0C;&#x4F7F;&#x7528; SQLite &#x5B58;&#x50A8;&#x6570;&#x636E;&#xFF0C;&#x91CD;&#x5199; onCreate&#x3001;query&#x3001;insert&#x3001;update&#x3001;delete &#x65B9;&#x6CD5;&#xFF0C;&#x4F7F;&#x7528; UriMatcher &#x4E0E; &#x6307;&#x5B9A;&#x7684; Uri &#x5173;&#x8054;&#x5E76;&#x6307;&#x5B9A; Uri_Code</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookContentProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ContentProvider</span> </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String TAG = <span class="hljs-string">&quot;borax-provider&quot;</span>;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String AUTHORITY = <span class="hljs-string">&quot;com.borax.helloworld.provider.BookContentProvider&quot;</span>;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Uri BOOK_CONTENT_URI = Uri.parse(<span class="hljs-string">&quot;content://&quot;</span> + AUTHORITY + <span class="hljs-string">&quot;/book&quot;</span>);
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Uri USER_CONTENT_URI = Uri.parse(<span class="hljs-string">&quot;content://&quot;</span> + AUTHORITY + <span class="hljs-string">&quot;/user&quot;</span>);

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> UriMatcher uriMatcher = <span class="hljs-keyword">new</span> UriMatcher(UriMatcher.NO_MATCH);

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> BOOK_URI_CODE = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> USER_URI_CODE = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">static</span> {
        uriMatcher.addURI(AUTHORITY, <span class="hljs-string">&quot;book&quot;</span>, BOOK_URI_CODE);
        uriMatcher.addURI(AUTHORITY, <span class="hljs-string">&quot;user&quot;</span>, USER_URI_CODE);
    }

    <span class="hljs-keyword">private</span> SQLiteDatabase db;

    <span class="hljs-function"><span class="hljs-keyword">private</span> String <span class="hljs-title">getTableName</span><span class="hljs-params">(Uri uri)</span> </span>{

        <span class="hljs-keyword">switch</span> (uriMatcher.match(uri)) {

            <span class="hljs-keyword">case</span> BOOK_URI_CODE: {
                <span class="hljs-keyword">return</span> DbOpenHelper.BOOK_TABLE_NAME;
            }

            <span class="hljs-keyword">case</span> USER_URI_CODE: {
                <span class="hljs-keyword">return</span> DbOpenHelper.USER_TABLE_NAME;
            }
        }

        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">onCreate</span><span class="hljs-params">()</span> </span>{

        db = <span class="hljs-keyword">new</span> DbOpenHelper(getContext()).getWritableDatabase();

        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }

    <span class="hljs-meta">@Nullable</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Cursor <span class="hljs-title">query</span><span class="hljs-params">(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder)</span> </span>{

        String table = getTableName(uri);

        <span class="hljs-keyword">return</span> db.query(table, projection, selection, selectionArgs, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>, sortOrder);
    }

    <span class="hljs-meta">@Nullable</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getType</span><span class="hljs-params">(@NonNull Uri uri)</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
    }

    <span class="hljs-meta">@Nullable</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Uri <span class="hljs-title">insert</span><span class="hljs-params">(@NonNull Uri uri, @Nullable ContentValues values)</span> </span>{

        db.insert(getTableName(uri), <span class="hljs-keyword">null</span>, values);
        getContext().getContentResolver().notifyChange(uri, <span class="hljs-keyword">null</span>);

        <span class="hljs-keyword">return</span> uri;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">delete</span><span class="hljs-params">(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs)</span> </span>{

        <span class="hljs-keyword">int</span> count = db.delete(getTableName(uri), selection, selectionArgs);

        <span class="hljs-keyword">if</span> (count &gt; <span class="hljs-number">0</span>) {
            getContext().getContentResolver().notifyChange(uri, <span class="hljs-keyword">null</span>);
        }

        <span class="hljs-keyword">return</span> count;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">update</span><span class="hljs-params">(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs)</span> </span>{

        <span class="hljs-keyword">int</span> count = db.update(getTableName(uri), values, selection, selectionArgs);

        <span class="hljs-keyword">if</span> (count &gt; <span class="hljs-number">0</span>) {
            getContext().getContentResolver().notifyChange(uri, <span class="hljs-keyword">null</span>);
        }

        <span class="hljs-keyword">return</span> count;
    }
}
</code></pre>
<p>&#x521B;&#x5EFA; SQLiteOpenHelper</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DbOpenHelper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SQLiteOpenHelper</span> </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String DB_NAME = <span class="hljs-string">&quot;book_content_provider.db&quot;</span>;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String BOOK_TABLE_NAME = <span class="hljs-string">&quot;book&quot;</span>;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String USER_TABLE_NAME = <span class="hljs-string">&quot;user&quot;</span>;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> DB_VERSION = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">private</span> String CREATE_BOOK_TABLE = <span class="hljs-string">&quot;create table if not exists &quot;</span> + BOOK_TABLE_NAME + <span class="hljs-string">&quot;(id INTEGER PRIMARY KEY, name TEXT)&quot;</span>;
    <span class="hljs-keyword">private</span> String CREATE_USER_TABLE = <span class="hljs-string">&quot;create table if not exists &quot;</span> + USER_TABLE_NAME + <span class="hljs-string">&quot;(id INTEGER PRIMARY KEY, name TEXT, sex INT)&quot;</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">DbOpenHelper</span><span class="hljs-params">(Context context)</span> </span>{
        <span class="hljs-keyword">super</span>(context, DB_NAME, <span class="hljs-keyword">null</span>, DB_VERSION);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(SQLiteDatabase db)</span> </span>{

        db.execSQL(CREATE_BOOK_TABLE);
        db.execSQL(CREATE_USER_TABLE);

    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onUpgrade</span><span class="hljs-params">(SQLiteDatabase db, <span class="hljs-keyword">int</span> oldVersion, <span class="hljs-keyword">int</span> newVersion)</span> </span>{

    }
}
</code></pre>
<p>&#x5728; AndroidManifest &#x4E2D;&#x6CE8;&#x518C; ContentProvider</p>
<pre><code class="lang-java">&lt;provider
    android:name=<span class="hljs-string">&quot;com.borax.helloworld.contentprovider.demo.BookContentProvider&quot;</span>
    android:authorities=<span class="hljs-string">&quot;com.borax.helloworld.provider.BookContentProvider&quot;</span>
    android:process=<span class="hljs-string">&quot;:provider&quot;</span> /&gt;
</code></pre>
<p>&#x5728; Activity &#x4E2D;&#x901A;&#x8FC7; getContentResolver &#x65B9;&#x6CD5;&#x83B7;&#x5F97; ContentResolver&#xFF0C;&#x5E76;&#x6839;&#x636E;&#x9700;&#x6C42;&#x8C03;&#x7528; insert&#x3001;query&#x3001;update&#x3001;delete &#x65B9;&#x6CD5;</p>
<pre><code class="lang-java"><span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Uri bookUri = Uri.parse(<span class="hljs-string">&quot;content://com.borax.helloworld.provider.BookContentProvider/book&quot;</span>);
        Uri userUri = Uri.parse(<span class="hljs-string">&quot;content://com.borax.helloworld.provider.BookContentProvider/user&quot;</span>);

        <span class="hljs-comment">//insert</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
            ContentValues contentValues = <span class="hljs-keyword">new</span> ContentValues();
            contentValues.put(<span class="hljs-string">&quot;id&quot;</span>, i);
            contentValues.put(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Android Interview &quot;</span> + i);
            getContentResolver().insert(bookUri, contentValues);
        }

        <span class="hljs-comment">//query</span>
        Cursor bookCursor = getContentResolver().query(bookUri, <span class="hljs-keyword">new</span> String[]{<span class="hljs-string">&quot;id&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>}, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>);
        <span class="hljs-keyword">while</span> (bookCursor.moveToNext()) {
            Book book = <span class="hljs-keyword">new</span> Book(bookCursor.getInt(<span class="hljs-number">0</span>), bookCursor.getString(<span class="hljs-number">1</span>));
            Log.e(<span class="hljs-string">&quot;borax-MainActivity&quot;</span>, book.toString());
        }
        bookCursor.close();

    }
</code></pre>
<h3 id="&#x8BF4;&#x8BF4;contentprovider&#x3001;contentresolver&#x3001;contentobserver-&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;">&#x8BF4;&#x8BF4;ContentProvider&#x3001;ContentResolver&#x3001;ContentObserver &#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;</h3>
<ul>
<li><p>ContentResolver &#x7684;&#x4F5C;&#x7528;</p>
<ul>
<li><p>&#x7EDF;&#x4E00;&#x7BA1;&#x7406;&#x4E0D;&#x540C; ContentProvider &#x4E4B;&#x95F4;&#x7684;&#x64CD;&#x4F5C;</p>
</li>
<li><p>&#x901A;&#x8FC7; Uri &#x5373;&#x53EF;&#x64CD;&#x4F5C;&#x4E0D;&#x540C; ContentProvider &#x4E2D;&#x7684;&#x6570;&#x636E;</p>
</li>
<li>&#x901A;&#x8FC7; ContentResolver &#x4ECE;&#x800C;&#x4E0E; ContentProvider &#x8FDB;&#x884C;&#x4EA4;&#x4E92;</li>
</ul>
</li>
<li><p>ContentObserver &#x7684;&#x4F5C;&#x7528;</p>
<p>&#x89C2;&#x5BDF; Uri &#x5F15;&#x8D77; ContentProvider &#x4E2D;&#x6570;&#x636E;&#x7684;&#x53D8;&#x5316;&#x4ECE;&#x800C;&#x901A;&#x77E5;&#x5916;&#x754C;&#xFF0C;&#x5F53; ContentProvider &#x4E2D;&#x7684;&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x5C31;&#x4F1A;&#x89E6;&#x53D1; ContentObserver &#x7C7B;&#x3002;&#x4F7F;&#x7528;&#xFF1A;</p>
</li>
</ul>
<pre><code class="lang-java">getContentResolver().registerContentObserver(bookUri, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">new</span> ContentObserver(handler) {
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">deliverSelfNotifications</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">super</span>.deliverSelfNotifications();
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onChange</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> selfChange)</span> </span>{
        <span class="hljs-keyword">super</span>.onChange(selfChange);
        Log.e(<span class="hljs-string">&quot;borax-MainActivity&quot;</span>, <span class="hljs-string">&quot;onChange&quot;</span>);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onChange</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> selfChange, Uri uri)</span> </span>{
        <span class="hljs-keyword">super</span>.onChange(selfChange, uri);
        }
});
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Service.html" class="navigation navigation-prev " aria-label="Previous page: Service">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="BroadcastReceiver.html" class="navigation navigation-next " aria-label="Next page: BroadcastReceiver">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"ContentProvider","level":"1.2.1.3","depth":3,"next":{"title":"BroadcastReceiver","level":"1.2.1.4","depth":3,"path":"Android/四大组件/BroadcastReceiver.md","ref":"Android/四大组件/BroadcastReceiver.md","articles":[]},"previous":{"title":"Service","level":"1.2.1.2","depth":3,"path":"Android/四大组件/Service.md","ref":"Android/四大组件/Service.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Android/四大组件/ContentProvider.md","mtime":"2019-01-21T04:22:20.368Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-01-21T14:04:13.553Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

